//
//  ViewController.m
//  hotSearchForOC
//
//  Created by Xiang on 2021/5/15.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *titles;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupLayoutViews];
}

-(void)setupLayoutViews{
    
    CGFloat x = 15;
    CGFloat y = 10 + 40;
    
    for (int i = 0; i < self.titles.count; i++) {
        
        UILabel *label = [self labelWithTitle:self.titles[i]];
        CGFloat width = label.frame.size.width + 30;
        
        if (x + width + 15 > self.view.bounds.size.width) {
            y += 40;//换行
            x = 15; //15位置开始
        }
        
        label.frame = CGRectMake(x, y, width, 30);
        [self.view addSubview:label];
        x += width + 10;//宽度+间隙
    }
}



- (UILabel *)labelWithTitle:(NSString *)title{
    
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    [label sizeToFit];
    label.layer.cornerRadius = 3;
    label.layer.borderWidth = 0.5;
    label.userInteractionEnabled = YES;
    label.font = [UIFont systemFontOfSize:13];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}


- (NSArray *)titles{
    if (_titles == nil) {
        _titles = @[@"苹果",@"apple",@"桃子",@"peach",@"梨子",@"pear",@"橘子",@"orange",@"葡萄",@"grape",@"花生",@"peanut",@"香蕉",@"banana",@"咖啡",@"coffee"];
    }
    return _titles;
}

@end
